package com.yangyang.androidconnect.phoneinfo;


import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract;

import com.yangyang.androidconnect.phoneinfo.model.CallRecord;
import com.yangyang.androidconnect.phoneinfo.model.Contact;
import com.yangyang.androidconnect.phoneinfo.model.SMSRecord;

import java.util.ArrayList;
import java.util.List;

// DataCollectionService.java
public class DataCollectionService {
    private Context context;

    public DataCollectionService(Context context) {
        this.context = context.getApplicationContext();
    }

    /**
     * 获取所有联系人
     */
    public List<Contact> getAllContacts() {
        List<Contact> contacts = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();

        String[] projection = {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
        };

        Cursor cursor = contentResolver.query(
                ContactsContract.Contacts.CONTENT_URI,
                projection,
                null,
                null,
                ContactsContract.Contacts.DISPLAY_NAME + " ASC"
        );

        if (cursor != null) {
            try {
                int idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                int hasPhoneIndex = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);

                while (cursor.moveToNext()) {
                    String id = cursor.getString(idIndex);
                    String name = cursor.getString(nameIndex);
                    int hasPhone = cursor.getInt(hasPhoneIndex);

                    List<String> phoneNumbers = new ArrayList<>();
                    if (hasPhone > 0) {
                        phoneNumbers = getPhoneNumbers(id);
                    }

                    contacts.add(new Contact(id, name, phoneNumbers));
                }
            } finally {
                cursor.close();
            }
        }

        return contacts;
    }

    private List<String> getPhoneNumbers(String contactId) {
        List<String> phoneNumbers = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();

        Cursor phoneCursor = contentResolver.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[]{contactId},
                null
        );

        if (phoneCursor != null) {
            try {
                int numberIndex = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                while (phoneCursor.moveToNext()) {
                    String number = phoneCursor.getString(numberIndex);
                    if (number != null) {
                        phoneNumbers.add(number.replaceAll("\\s+", "")); // 移除空格
                    }
                }
            } finally {
                phoneCursor.close();
            }
        }

        return phoneNumbers;
    }

    /**
     * 获取通话记录
     */
    public List<CallRecord> getCallLogs() {
        List<CallRecord> callRecords = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();

        String[] projection = {
                CallLog.Calls.NUMBER,
                CallLog.Calls.DATE,
                CallLog.Calls.DURATION,
                CallLog.Calls.TYPE
        };

        Cursor cursor = contentResolver.query(
                CallLog.Calls.CONTENT_URI,
                projection,
                null,
                null,
                CallLog.Calls.DATE + " DESC"
        );

        if (cursor != null) {
            try {
                int numberIndex = cursor.getColumnIndex(CallLog.Calls.NUMBER);
                int dateIndex = cursor.getColumnIndex(CallLog.Calls.DATE);
                int durationIndex = cursor.getColumnIndex(CallLog.Calls.DURATION);
                int typeIndex = cursor.getColumnIndex(CallLog.Calls.TYPE);

                while (cursor.moveToNext()) {
                    String number = cursor.getString(numberIndex);
                    if (number == null) number = "Unknown";

                    long date = cursor.getLong(dateIndex);
                    long duration = cursor.getLong(durationIndex);
                    int type = cursor.getInt(typeIndex);

                    callRecords.add(new CallRecord(number, date, duration, type));
                }
            } finally {
                cursor.close();
            }
        }

        return callRecords;
    }

    /**
     * 获取短信记录
     */
    public List<SMSRecord> getSMSRecords() {
        List<SMSRecord> smsRecords = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();

        Uri inboxUri = Uri.parse("content://sms/inbox");
        String[] projection = {"_id", "address", "body", "date", "type"};

        Cursor cursor = contentResolver.query(
                inboxUri,
                projection,
                null,
                null,
                "date DESC"
        );

        if (cursor != null) {
            try {
                int addressIndex = cursor.getColumnIndex("address");
                int bodyIndex = cursor.getColumnIndex("body");
                int dateIndex = cursor.getColumnIndex("date");
                int typeIndex = cursor.getColumnIndex("type");

                while (cursor.moveToNext()) {
                    String address = cursor.getString(addressIndex);
                    if (address == null) address = "Unknown";

                    String body = cursor.getString(bodyIndex);
                    if (body == null) body = "";

                    long date = cursor.getLong(dateIndex);
                    int type = cursor.getInt(typeIndex);

                    smsRecords.add(new SMSRecord(address, body, date, type));
                }
            } finally {
                cursor.close();
            }
        }

        return smsRecords;
    }
}